package com.phicomm.cloud.senseTime;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

public class ImageVerificationStateless {
    public final String POST_URL = "https://v2-auth-api.visioncloudapi.com/identity/image_verification/stateless";

    public String httpClientPost(String filepath1, String filepath2) throws ClientProtocolException, IOException {
        String result="";
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost post = new HttpPost(POST_URL);
        FileBody fileBody1 = new FileBody(new File(filepath1));
        FileBody fileBody2 = new FileBody(new File(filepath2));
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("first_image_file", fileBody1);
        entity.addPart("second_image_file", fileBody2);
        post.setEntity(entity);
        post.setHeader("Authorization", "key=d18b659e874a40fd9034627ec14489ae,timestamp=1515727313150,nonce=CMfP90eR3loEtLcS,signature=e9f8fedd187343327222bec1bf845e3762b32c9ec211c4b5f735f25b63273a11");//请将AUTHORIZATION替换为根据API_KEY和API_SECRET得到的签名认证串
        HttpResponse response = httpclient.execute(post);
        if (response.getStatusLine().getStatusCode() == 200) {
            HttpEntity entitys = response.getEntity();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(entitys.getContent()));
            String line = reader.readLine();
            System.out.println(line);
            result=line;
        }else{
            HttpEntity r_entity = response.getEntity();
            String responseString = EntityUtils.toString(r_entity);
            System.out.println("错误码是："+response.getStatusLine().getStatusCode()+"  "+response.getStatusLine().getReasonPhrase());
            System.out.println("出错原因是："+responseString);
            //你需要根据出错的原因判断错误信息，并修改

            result=responseString;
        }

        httpclient.getConnectionManager().shutdown();

        return result;
    }

}